package com.chub.study.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author : chub
 * @version : V1.0.0
 * @fileName : CustomDateJsonSerializer.java
 * @description : 日期序列化工具,直接将时间类型的转为yyyy-MM-dd类型的数据
 * @createTime : 2021-10-27 14:12
 */
public class CustomDateJsonSerializer extends JsonSerializer<LocalDateTime> {

    //定义日期格式
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
//        gen.writeString(sdf.format(value));
        gen.writeString(value.format(formatter));
    }
}
